package top.cyuw.subjectsystem.aspect;

import top.cyuw.subjectsystem.dto.BaseDto;
import top.cyuw.subjectsystem.exception.ServiceException;
import top.cyuw.subjectsystem.util.ValidationUtils;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Component;

@Aspect
@Component
public class AccessAspect {

    @Pointcut("@annotation(org.springframework.web.bind.annotation.RequestMapping)")
    public void pointCutMethod() {

    }

    @Before("pointCutMethod()")
    public void doBefore(JoinPoint joinPoint) {
        for (Object arg : joinPoint.getArgs()) {
            if (arg instanceof BaseDto) {
                String result = ValidationUtils.validateEntity(arg);
                if (result != null) {
                    throw new ServiceException(400, result);
                }
            }
        }
    }

}
